<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2155 – Declare and assign separately to avoid masking return values.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2155 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2155">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="declare-and-assign-separately-to-avoid-masking-return-values">Declare
and assign separately to avoid masking return values.</h2>
<h3 id="problematic-code-in-the-case-of-export">Problematic code in the
case of <code>export</code>:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2155.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">foo</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h4 id="correct-code">Correct code:</h4>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2155.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb2-2"><a href="SC2155.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">foo</span></span></code></pre></div>
<h4 id="rationale">Rationale:</h4>
<p>In the original code, the return value of <code>mycmd</code> is
ignored, and <code>export</code> will instead always return true. This
may prevent conditionals, <code>set -e</code> and traps from working
correctly.</p>
<p>When first marked for export and assigned separately, the return
value of the assignment will be that of <code>mycmd</code>. This avoids
the problem.</p>
<p>Note that ShellCheck does not warn about masking of local read-only
variables, such as <code>local -r foo=$(cmd)</code>, even though this
also masks the return value. This is because the alternative
<code>local foo; foo=$(cmd); local -r foo</code> is repetitive and
cumbersome. To see warnings for this and many other additional cases of
suppressed exit codes, enable
<code>check-extra-masked-returns</code>.</p>
<h4 id="exceptions">Exceptions:</h4>
<p>If you intend to ignore the return value of an assignment, you can
either <a href="ignore.html">ignore</a> this warning or use</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2155.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span> <span class="kw">||</span> <span class="fu">true</span></span>
<span id="cb3-2"><a href="SC2155.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">foo</span></span></code></pre></div>
<p>Shellcheck does not warn about <code>export foo=bar</code> because
<code>bar</code> is a literal and not a command substitution with an
independent return value.</p>
<h3 id="problematic-code-in-the-case-of-local">Problematic code in the
case of <code>local</code>:</h3>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2155.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="bu">local</span> <span class="va">foo</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h4 id="correct-code-1">Correct code:</h4>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="SC2155.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="bu">local</span> <span class="va">foo</span></span>
<span id="cb5-2"><a href="SC2155.html#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span></span></code></pre></div>
<h4 id="rationale-1">Rationale</h4>
<p>The exit status of the command is overridden by the exit status of
the creation of the local variable. For example:</p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode bash"><code class="sourceCode bash"><span id="cb6-1"><a href="SC2155.html#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> f<span class="er">(</span><span class="kw">)</span> <span class="kw">{</span> <span class="bu">local</span> <span class="va">foo</span><span class="op">=</span><span class="va">$(</span><span class="fu">false</span><span class="va">)</span> <span class="kw">&amp;&amp;</span> <span class="bu">echo</span> <span class="st">&quot;error was hidden&quot;</span><span class="kw">;</span> <span class="kw">};</span> <span class="ex">f</span></span>
<span id="cb6-2"><a href="SC2155.html#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="ex">error</span> was hidden</span>
<span id="cb6-3"><a href="SC2155.html#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> f<span class="er">(</span><span class="kw">)</span> <span class="kw">{</span> <span class="bu">local</span> <span class="va">foo</span><span class="kw">;</span> <span class="va">foo</span><span class="op">=</span><span class="va">$(</span><span class="fu">false</span><span class="va">)</span> <span class="kw">&amp;&amp;</span> <span class="bu">echo</span> <span class="st">&quot;error was hidden&quot;</span><span class="kw">;</span> <span class="kw">};</span> <span class="ex">f</span></span></code></pre></div>
<h3 id="problematic-code-in-the-case-of-readonly">Problematic code in
the case of <code>readonly</code>:</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb7-1"><a href="SC2155.html#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="bu">readonly</span> <span class="va">foo</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h4 id="correct-code-2">Correct code:</h4>
<div class="sourceCode" id="cb8"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb8-1"><a href="SC2155.html#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="va">foo</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="ex">mycmd</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb8-2"><a href="SC2155.html#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="bu">readonly</span> <span class="va">foo</span></span></code></pre></div>
<h2 id="word-splitting-and-quoting-issue-with-dash-maybe-others">Word
splitting and quoting issue with dash, maybe others</h2>
<p>A serious quoting problem with dash is another reason to declare and
assign separately. Dash is the <a
href="https://wiki.ubuntu.com/DashAsBinSh">default, <code>/bin/sh</code>
shell on Ubuntu</a>. More specifically, dash version 0.5.8-2.10 and
others cannot run these two examples:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb9-1"><a href="SC2155.html#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f()</span><span class="kw">{</span> <span class="bu">local</span> <span class="va">e</span><span class="op">=</span><span class="va">$1</span><span class="kw">;</span> <span class="kw">}</span></span>
<span id="cb9-2"><a href="SC2155.html#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="ex">f</span> <span class="st">&quot;1 2&quot;</span></span>
<span id="cb9-3"><a href="SC2155.html#cb9-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-4"><a href="SC2155.html#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="bu">export</span> <span class="va">g</span><span class="op">=</span><span class="va">$(</span><span class="bu">printf</span> <span class="st">&#39;%s&#39;</span> <span class="st">&quot;foo 2&quot;</span><span class="va">)</span></span></code></pre></div>
<p>While this runs fine in other shells, <a
href="http://mywiki.wooledge.org/BashPitfalls#local_var.3D.24.28cmd.29">dash
doesn't treat any of these as assignments</a> and fails both like
this:</p>
<pre><code>local: 2: bad variable name
export: 2: bad variable name</code></pre>
<p>The direct workaround to this bug is to quote the right-hand-side of
the assignment. Separating declaraction and assignment also makes this
runs fine in any shell.</p>
<p>(A rule to catch this problem is in the works at <a
href="https://github.com/koalaman/shellcheck/issues/1556">#1556</a>).</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


